package com.banggood.upms.converter;

import com.banggood.upms.bean.po.Permission;
import com.banggood.upms.bean.po.SystemPo;
import com.banggood.upms.bean.vo.PermissionVo;
import com.banggood.upms.bean.vo.SystemModuleVo;
import com.banggood.upms.bean.vo.SystemVo;
import com.banggood.upms.common.util.TimeUtils;
import com.banggood.upms.common.component.CommonComponent;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XuChuangFeng
 * @date 2017/7/4
 */
@Component
public class SystemConverter {

    @Autowired
    private CommonComponent commonComponent;
    
    public SystemPo singleInputConverter(SystemVo vo) {
        return voToPo(vo);
    }

    public List<SystemPo> inputConverter(List<SystemVo> voList) {
        List<SystemPo> poList = new ArrayList<>();
        for(SystemVo vo : voList) {
            poList.add(voToPo(vo));
        }
        return poList;
    }

    public List<SystemVo> outputConverter(List<SystemPo> poList) {
        List<SystemVo> voList = new ArrayList<>();
        for(SystemPo po : poList) {
            voList.add(poToVo(po));
        }
        return voList;
    }

    public SystemVo singleOutputConverter(SystemPo po) {
        if(po == null) {
            return null;
        }
        return poToVo(po);
    }

    private SystemVo poToVo(SystemPo po) {
        SystemVo vo = new SystemVo();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    private SystemPo voToPo(SystemVo systemVo) {
        SystemPo systemPo = sysTransferToPo(systemVo);

        List<Permission> permissions = extractPerms(systemVo);
        systemPo.setPermissionList(permissions);
        return systemPo;
    }

    public SystemVo mergeSysInfoAndPermInfo(SystemVo inboundSys, SystemVo metaSysInfo) {
        //防止覆盖meta中的权限列表
        inboundSys.setModules(metaSysInfo.getModules());
        BeanUtils.copyProperties(inboundSys, metaSysInfo);
        metaSysInfo.setLastSyncTime(TimeUtils.getCurrentTimeStr());
        return metaSysInfo;
    }

    private Permission mergePermVoToPo(SystemVo sysVo, SystemModuleVo moduleVo, PermissionVo permVo) {
        Permission permPo = new Permission();
        BeanUtils.copyProperties(permVo, permPo);
        permPo.setPermissionId(commonComponent.generateId());
        permPo.setSystemId(sysVo.getSystemId());
        permPo.setModuleCode(moduleVo.getCode());
        permPo.setModuleName(moduleVo.getName());
        return permPo;
}

    private SystemPo sysTransferToPo(SystemVo vo) {
        SystemPo po = new SystemPo();
        BeanUtils.copyProperties(vo, po);
        return po;
    }

    public List<Permission> extractPerms(SystemVo systemVo) {
        if(!CollectionUtils.isEmpty(systemVo.getModules())) {
            List<Permission> permPos = new ArrayList<>();
            //系统、模块跟权限的层次结构：系统->N个模块->N个权限
            for(SystemModuleVo moduleVo : systemVo.getModules()) {
                if(!CollectionUtils.isEmpty(moduleVo.getPermissionVos())) {
                    for(PermissionVo vo : moduleVo.getPermissionVos()) {
                        Permission permPo = mergePermVoToPo(systemVo, moduleVo, vo);
                        permPos.add(permPo);
                    }
                }
            }
            return permPos;
        }
        return null;
    }
}
